﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using WP_Customer_ShopMob.ViewModels;
using System.Collections.ObjectModel;

namespace WP_Customer_ShopMob.Views
{
    public partial class SousCategoriesPage : PhoneApplicationPage
    {
        WP_Customer_ShopMob.Model.Repository repo = new WP_Customer_ShopMob.Model.Repository();
        public SousCategoriesPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string idCateg;
            NavigationContext.QueryString.TryGetValue("id", out idCateg);
            long id = long.Parse(idCateg);
            CategorieViewModel cvm = Helper.IsolatedStorage.Load<ObservableCollection<CategorieViewModel>>("categories").SingleOrDefault(x => x.Id == id);
            if (!cvm.IsDataLoaded)
            {
                cvm.LoadData(NavigationService);
            }
            else
            {
                var categs = Helper.IsolatedStorage.Load<ObservableCollection<CategorieViewModel>>("categories").Where(x => x.IdMere == id);
                foreach (var categ in categs)
                {
                    cvm.ListSousCategories.Add(categ);
                }
            }
            this.DataContext = cvm;
        }

        private void lst_categorie_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (lst_categories.SelectedItem != null)
            {
                ViewModels.CategorieViewModel cvm = (lst_categories.SelectedItem as ViewModels.CategorieViewModel);
                if (!cvm.IsDataLoaded)
                {
                    cvm.LoadData(NavigationService);
                }
                //while (!cvm.IsDataLoaded) { }
                //var categs = Helper.IsolatedStorage.Load<ObservableCollection<ViewModels.CategorieViewModel>>("categories").Where(x => x.IdMere == cvm.Id).Count();
                //var arts = Helper.IsolatedStorage.Load<ObservableCollection<ViewModels.ArticleViewModel>>("articles");
                //if (categs != 0)
                //{
                //    NavigationService.Navigate(new Uri("/Views/sousCategoriesPage.xaml?id=" + cvm.Id, UriKind.Relative));
                //}
                //else if (arts != null && arts.Where(x => x.IdCategorie == cvm.Id).Count() != 0)
                //{
                //    NavigationService.Navigate(new Uri("/Views/CategoriesPage.xaml?id=" + cvm.Id, UriKind.Relative));
                //}
                //else
                //{
                //    alert("Aucun contenu disponible");
                //}
                lst_categories.SelectedItem = null;
            }
        }

        private void alert(string p)
        {
            MessageBox.Show(p);
        }

        private void lst_article_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            /*if (lst_articles.SelectedItem != null)
            {
                string id = (lst_articles.SelectedItem as ArticleViewModel).IdArticle;
                NavigationService.Navigate(new Uri("/Views/ArticlesPage.xaml?id=" + id, UriKind.Relative));
                lst_articles.SelectedItem = null;
            }*/
        }

        private void pivot_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            /*if (!(pivot.SelectedItem as CategorieViewModel).IsDataLoaded)
            {
                (pivot.SelectedItem as CategorieViewModel).LoadData();
            }*/
        }
    }
}